/**
 * Project Name:cnsy-url-short-service
 * File Name:FileUploadLogServiceImpl.java
 * Package Name:cn.cnsy123.shorturls.business.service.impl 
 * Date: 2020年4月6日-下午11:41:31
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.cnsy123.shorturls.api.controller.vo.SaveFileUploadLogResult;
import cn.cnsy123.shorturls.business.entity.SysFileUploadLogWithBLOBs;
import cn.cnsy123.shorturls.business.exception.BusinessException;
import cn.cnsy123.shorturls.business.mapper.SysFileUploadLogMapper;
import cn.cnsy123.shorturls.business.service.FileUploadLogService;

/**
 * ClassName: FileUploadLogServiceImpl
 * 
 * @author yangpengfei
 * @date 2020年4月6日-下午11:41:31
 * @version 1.0
 * @since JDK 1.7
 */
@Service
public class FileUploadLogServiceImpl implements FileUploadLogService {
    
    @Autowired
    private SysFileUploadLogMapper sysFileUploadLogMapper;

    @Override
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class)
    public SaveFileUploadLogResult saveFileUploadLog(String originalName, String fileName, String filePath,
            String fileExtension, String batchNumber, boolean uploadStatus, String fileMD5Value) {
        SaveFileUploadLogResult saveFileUploadLogResult = new SaveFileUploadLogResult();
        SysFileUploadLogWithBLOBs record = new SysFileUploadLogWithBLOBs();
        record.setBatchNumber(batchNumber);
        record.setCreateTime(new Date(System.currentTimeMillis()));
        record.setCreateUser("系统管理员");
        record.setFilePath(filePath.getBytes(StandardCharsets.UTF_8));
        record.setFileType(fileExtension);
        record.setOriginalName(originalName);
        record.setUploadName(fileName);
        record.setUploadStatus(uploadStatus);
        record.setRemarkInfo(("BATCH_NUMBER:["+batchNumber+"],UPLOADED FILE NAME:["+fileName+"]").getBytes(StandardCharsets.UTF_8));
        record.setFileValidateCode(fileMD5Value);
        int flag = sysFileUploadLogMapper.insertSelective(record);
        if(flag > 0) {
            SysFileUploadLogWithBLOBs sysFileUploadLog = sysFileUploadLogMapper.selectOneByNewFileName(fileName);
            saveFileUploadLogResult.setCode(200);
            saveFileUploadLogResult.setMsg("保存记录成功");
            saveFileUploadLogResult.setFileId(sysFileUploadLog.getId());
        }
        return saveFileUploadLogResult;
    }

    @Override
    @Transactional(readOnly = false,propagation = Propagation.REQUIRED,rollbackFor = BusinessException.class)
    public SaveFileUploadLogResult queryFileUploadLogByfileMD5(String fileMd5Value) {
        SaveFileUploadLogResult saveFileUploadLogResult = new SaveFileUploadLogResult();
        SysFileUploadLogWithBLOBs sysFileUploadLog = sysFileUploadLogMapper.selectOneByFileMd5Value(fileMd5Value);
        if(sysFileUploadLog != null) {
            saveFileUploadLogResult.setFileId(sysFileUploadLog.getId());
        }
        return saveFileUploadLogResult;
    }

}
